export default async function request({
  url,
  method = "GET",
  params = {},
  data = {},
  ...restProps
}) {
  let realUrl = url;
  const options: any = {
    method,
    ...restProps,
  };
  if (method === "POST" || method === "PUT") {
    options.headers = {
      "Content-Type": "application/json",
    };
    options.body = JSON.stringify(data);
  }
  if (
    (method === "GET" || method === "DELETE") &&
    Object.keys(params).length > 0
  ) {
    realUrl += "?" + new URLSearchParams(params).toString();
  }
  // 浏览器和node通用环境变量 NEXT_PUBLIC_ 开头
  return fetch(process.env.NEXT_PUBLIC_BASE_URL + realUrl, options).then(
    (res) => res.json()
  );
}
